home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / bash / bash_108 / bash-108.zoo / bash-1.08 / bashline.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-19  |  31.1 KB  |  1,244 lines

  1. /* bashline.c -- Bash's interface to the readline library. */
  2.  
  3. /* Copyright (C) 1987,1991 Free Software Foundation, Inc.
  4.  
  5.    This file is part of GNU Bash, the Bourne Again SHell.
  6.  
  7.    Bash is free software; you can redistribute it and/or modify it
  8.    under the terms of the GNU General Public License as published by
  9.    the Free Software Foundation; either version 1, or (at your option)
  10.    any later version.
  11.  
  12.    Bash is distributed in the hope that it will be useful, but WITHOUT
  13.    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  14.    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
  15.    License for more details.
  16.  
  17.    You should have received a copy of the GNU General Public License
  18.    along with Bash; see the file COPYING.  If not, write to the Free
  19.    Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  20.  
  21. #include <stdio.h>
  22. #include <readline/readline.h>
  23. #include <readline/history.h>
  24. #include "shell.h"
  25. #include "builtins.h"
  26.  
  27. /* Functions bound to keys in Readline for Bash users. */
  28. static void
  29.   shell_expand_line (), insert_last_arg (), display_shell_version (),
  30.   operate_and_get_next ();
  31.  
  32. /* Helper functions for Readline. */
  33. static void
  34.   bash_symbolic_link_hook (), filename_completion_ignore (), bash_push_line ();
  35.  
  36. static char
  37.   **attempt_shell_completion (), *bash_tilde_expand (),
  38.   *variable_completion_function (), *hostname_completion_function (),
  39.   *command_word_completion_function ();
  40.  
  41. static void
  42.   snarf_hosts_from_file (), add_host_name (), sort_hostname_list ();
  43.  
  44. /* Externally defined functions used by this file. */
  45. extern char
  46.   *get_string_value (), *filename_completion_function (),
  47.   *username_completion_function ();
  48.  
  49. extern int
  50.   show_shell_version ();
  51.  
  52. /* SPECIFIC_COMPLETION_FUNCTIONS specifies that we have individual
  53.    completion functions which indicate what type of completion should be
  54.    done (at or before point) that can be bound to key sequences with
  55.    the readline library. */
  56. #define SPECIFIC_COMPLETION_FUNCTIONS
  57.  
  58. #if defined (SPECIFIC_COMPLETION_FUNCTIONS)
  59. static void
  60.   bash_specific_completion (),
  61.   bash_complete_filename (), bash_possible_filename_completions (),
  62.   bash_complete_filename_internal (),
  63.   bash_complete_username (), bash_possible_username_completions (),
  64.   bash_complete_username_internal (),
  65.   bash_complete_hostname (), bash_possible_hostname_completions (),
  66.   bash_complete_hostname_internal (),
  67.   bash_complete_variable (), bash_possible_variable_completions (),
  68.   bash_complete_variable_internal (),
  69.   bash_complete_command (), bash_possible_command_completions (),
  70.   bash_complete_command_internal ();
  71. #endif /* SPECIFIC_COMPLETION_FUNCTIONS */
  72.  
  73. /* Called once from parse.y if we are going to use readline. */
  74. initialize_readline ()
  75. {
  76.   rl_terminal_name = get_string_value ("TERM");
  77.   rl_instream = stdin, rl_outstream = stderr;
  78.   rl_special_prefixes = "$@%";
  79.  
  80.   /* Allow conditional parsing of the ~/.inputrc file. */
  81.   rl_readline_name = "Bash";
  82.  
  83.   /* Bind up our special shell functions. */
  84.   rl_add_defun
  85.     ("shell-expand-line", (Function *)shell_expand_line, META(CTRL('E')));
  86.  
  87.   rl_add_defun
  88.     ("insert-last-argument", (Function *)insert_last_arg, META('.'));
  89.  
  90.   rl_bind_key (META('_'), (Function *)insert_last_arg);
  91.  
  92.   rl_add_defun
  93.     ("operate_and_get_next", (Function *)operate_and_get_next, CTRL('O'));
  94.  
  95.   rl_add_defun
  96.     ("display-shell-version", (Function *)display_shell_version, -1);
  97.  
  98.   rl_bind_key_in_map
  99.     (CTRL ('V'), (Function *)display_shell_version, emacs_ctlx_keymap);
  100.  
  101.   /* In Bash, the user can switch editing modes with "set -o [vi emacs]",
  102.      so it is not necessary to allow C-M-j for context switching.  Turn
  103.      off this occasionally confusing behaviour. */
  104.   rl_unbind_key_in_map (CTRL('J'), emacs_meta_keymap);
  105.  
  106. #ifdef SPECIFIC_COMPLETION_FUNCTIONS
  107.   rl_add_defun ("complete-filename", bash_complete_filename, META('/'));
  108.   rl_add_defun ("possible-filename-completions",
  109.         bash_possible_filename_completions, -1);
  110.   rl_bind_key_in_map ('/', bash_possible_filename_completions,
  111.               emacs_ctlx_keymap);
  112.  
  113.   rl_add_defun ("complete-username", bash_complete_username, META('~'));
  114.   rl_add_defun ("possible-username-completions",
  115.         bash_possible_username_completions, -1);
  116.   rl_bind_key_in_map ('~', bash_possible_username_completions,
  117.               emacs_ctlx_keymap);
  118.  
  119.   rl_add_defun ("complete-hostname", bash_complete_hostname, META('@'));
  120.   rl_add_defun ("possible-hostname-completions",
  121.         bash_possible_hostname_completions, -1);
  122.   rl_bind_key_in_map ('@', bash_possible_hostname_completions,
  123.               emacs_ctlx_keymap);
  124.  
  125.   rl_add_defun ("complete-variable", bash_complete_variable, META('$'));
  126.   rl_add_defun ("possible-variable-completions",
  127.         bash_possible_variable_completions, -1);
  128.   rl_bind_key_in_map ('$', bash_possible_variable_completions,
  129.               emacs_ctlx_keymap);
  130.  
  131.   rl_add_defun ("complete-command", bash_complete_command, META('!'));
  132.   rl_add_defun ("possible-command-completions",
  133.         bash_possible_command_completions, -1);
  134.   rl_bind_key_in_map ('!', bash_possible_command_completions,
  135.               emacs_ctlx_keymap);
  136.  
  137. #endif  /* SPECIFIC_COMPLETION_FUNCTIONS */
  138.  
  139.   /* Tell the completer that we want a crack first. */
  140.   rl_attempted_completion_function = (Function *)attempt_shell_completion;
  141.  
  142.   /* Tell the tilde expander that we want a crack if it fails. */
  143.   rl_tilde_expander = (Function *)bash_tilde_expand;
  144.  
  145.   /* Tell the completer that we might want to follow symbolic links. */
  146.   rl_symbolic_link_hook = (Function *)bash_symbolic_link_hook;
  147.  
  148.   /* Tell the filename completer we want a chance to ignore some names. */
  149.   rl_ignore_some_completions_function =
  150.     (Function *)filename_completion_ignore;
  151. }
  152.  
  153. /* On Sun systems at least, rl_attempted_completion_function can end up
  154.    getting set to NULL, and rl_completion_entry_function set to do command
  155.    word completion if Bash is interrupted while trying to complete a command
  156.    word.  This just resets all the completion functions to the right thing.
  157.    It's called from throw_to_top_level(). */
  158. bashline_reinitialize ()
  159. {
  160.   rl_attempted_completion_function = (Function *)attempt_shell_completion;
  161.   rl_tilde_expander = (Function *)bash_tilde_expand;
  162.   rl_symbolic_link_hook = (Function *)bash_symbolic_link_hook;
  163. }
  164.  
  165. /* Contains the line to push into readline. */
  166. static char *push_to_readline = (char *)NULL;
  167.  
  168. /* Push the contents of push_to_readline into the
  169.    readline buffer. */
  170. static void
  171. bash_push_line ()
  172. {
  173.   if (push_to_readline)
  174.     {
  175.       rl_insert_text (push_to_readline);
  176.       free (push_to_readline);
  177.       push_to_readline = (char *)NULL;
  178.     }
  179. }
  180.  
  181. /* Call this to set the initial text for the next line to read
  182.    from readline. */
  183. int
  184. bash_re_edit (line)
  185.      char *line;
  186. {
  187.   if (push_to_readline)
  188.     free (push_to_readline);
  189.  
  190.   push_to_readline = savestring (line);
  191.   rl_startup_hook = (Function *)bash_push_line;
  192.  
  193.   return (0);
  194. }
  195.  
  196. static void
  197. display_shell_version (count, c)
  198.      int count, c;
  199. {
  200.   crlf ();
  201.   show_shell_version ();
  202.   putc ('\r', rl_outstream);
  203.   fflush (rl_outstream);
  204.   rl_on_new_line ();
  205.   rl_redisplay ();
  206. }
  207.  
  208. /* **************************************************************** */
  209. /*                                    */
  210. /*                 Readline Stuff                */
  211. /*                                    */
  212. /* **************************************************************** */
  213.  
  214. /* If the user requests hostname completion, then simply build a list
  215.    of hosts, and complete from that forever more. */
  216. #if !defined (ETCHOSTS)
  217. #define ETCHOSTS "/etc/hosts"
  218. #endif
  219.  
  220. /* The kept list of hostnames. */
  221. static char **hostname_list = (char **)NULL;
  222.  
  223. /* The physical size of the above list. */
  224. static int hostname_list_size = 0;
  225.  
  226. /* The length of the above list. */
  227. static int hostname_list_length = 0;
  228.  
  229. /* Whether or not HOSTNAME_LIST has been initialized. */
  230. int hostname_list_initialized = 0;
  231.  
  232. /* Non-zero means that HOSTNAME_LIST needs to be sorted. */
  233. static int hostname_list_needs_sorting = 0;
  234.  
  235. /* Initialize the hostname completion table. */
  236. static void
  237. initialize_hostname_list ()
  238. {
  239.   char *temp = get_string_value ("hostname_completion_file");
  240.  
  241.   if (!temp)
  242.     temp = ETCHOSTS;
  243.  
  244.   snarf_hosts_from_file (temp);
  245.   sort_hostname_list ();
  246.   if (hostname_list)
  247.     hostname_list_initialized++;
  248. }
  249.  
  250. /* Add NAME to the list of hosts. */
  251. static void
  252. add_host_name (name)
  253.      char *name;
  254. {
  255.   if (hostname_list_length + 2 > hostname_list_size)
  256.     {
  257.       hostname_list = (char **)
  258.     xrealloc (hostname_list,
  259.           (1 + (hostname_list_size += 100)) * sizeof (char *));
  260.     }
  261.  
  262.   hostname_list[hostname_list_length] = savestring (name);
  263.   hostname_list[++hostname_list_length] = (char *)NULL;
  264.   hostname_list_needs_sorting++;
  265. }
  266.  
  267. /* After you have added some names, you should sort the list of names. */
  268. static void
  269. sort_hostname_list ()
  270. {
  271.   extern int qsort_string_compare ();
  272.   if (hostname_list_needs_sorting && hostname_list)
  273.     qsort (hostname_list, hostname_list_length,
  274.        sizeof (char *), qsort_string_compare);
  275.   hostname_list_needs_sorting = 0;
  276. }
  277.  
  278. #define cr_whitespace(c) ((c) == '\r' || (c) == '\n' || whitespace(c))
  279.  
  280. static void
  281. snarf_hosts_from_file (filename)
  282.      char *filename;
  283. {
  284.   FILE *file = fopen (filename, "r");
  285.   char *temp, buffer[256], name[256];
  286.   register int i, start;
  287.  
  288.   if (!file)
  289.     return;
  290.  
  291.   while (temp = fgets (buffer, 255, file))
  292.     {
  293.       /* Skip to first character. */
  294.       for (i = 0; buffer[i] && cr_whitespace (buffer[i]); i++);
  295.  
  296.       /* If comment, ignore. */
  297.       if (buffer[i] == '#')
  298.     continue;
  299.  
  300.       /* If `preprocessor' directive, do the include. */
  301.       if (strncmp (&buffer[i], "$include ", 9) == 0)
  302.     {
  303.       char *includefile = &buffer[i + 9];
  304.       char *t;
  305.  
  306.       /* Find start of filename. */
  307.       while (*includefile && whitespace (*includefile))
  308.         includefile++;
  309.  
  310.       t = includefile;
  311.  
  312.       /* Find end of filename. */
  313.       while (*t && !cr_whitespace (*t))
  314.         t++;
  315.  
  316.       *t = '\0';
  317.  
  318.       snarf_hosts_from_file (includefile);
  319.       continue;
  320.     }
  321.  
  322.       /* Skip internet address. */
  323.       for (; buffer[i] && !cr_whitespace (buffer[i]); i++);
  324.  
  325.       /* Gobble up names.  Each name is separated with whitespace. */
  326.       while (buffer[i] && buffer[i] != '#')
  327.     {
  328.       for (; i && cr_whitespace (buffer[i]); i++);
  329.       if (buffer[i] ==  '#')
  330.         continue;
  331.       for (start = i; buffer[i] && !cr_whitespace (buffer[i]); i++);
  332.       if ((i - start) == 0)
  333.         continue;
  334.       strncpy (name, buffer + start, i - start);
  335.       name[i - start] = '\0';
  336.       add_host_name (name);
  337.     }
  338.     }
  339.   fclose (file);
  340. }
  341.  
  342. /* Return a NULL terminated list of hostnames which begin with TEXT.
  343.    Initialize the hostname list the first time if neccessary.
  344.    The array is malloc ()'ed, but not the individual strings. */
  345. static char **
  346. hostnames_matching (text)
  347.      char *text;
  348. {
  349.   register int i, len = strlen (text);
  350.   register int begin, end;
  351.   int last_search = -1;
  352.   char **result = (char **)NULL;
  353.  
  354.   if (!hostname_list_initialized)
  355.     {
  356.       initialize_hostname_list ();
  357.  
  358.       if (!hostname_list_initialized)
  359.     return ((char **)NULL);
  360.     }
  361.  
  362.   sort_hostname_list ();
  363.  
  364.   /* The list is sorted.  Do a binary search on it for the first character
  365.      in TEXT, and then grovel the names of interest. */
  366.   begin = 0; end = hostname_list_length;
  367.  
  368.   /* Special case.  If TEXT consists of nothing, then the whole list is
  369.      what is desired. */
  370.   if (!*text)
  371.     {
  372.       result = (char **)xmalloc ((1 + hostname_list_length) * sizeof (char *));
  373.       for (i = 0; i < hostname_list_length; i++)
  374.     result[i] = hostname_list[i];
  375.       result[i] = (char *)NULL;
  376.       return (result);
  377.     }
  378.  
  379.   /* Scan until found, or failure. */
  380.   while (end != begin)
  381.     {
  382.       int r = 0;
  383.  
  384.       i = ((end - begin) / 2) + begin;
  385.       if (i == last_search)
  386.     break;
  387.  
  388.       if (hostname_list[i] &&
  389.       (r = strncmp (hostname_list[i], text, len)) == 0)
  390.     {
  391.       while (strncmp (hostname_list[i], text, len) == 0 && i) i--;
  392.       if (strncmp (hostname_list[i], text, len) != 0) i++;
  393.  
  394.       begin = i;
  395.       while (hostname_list[i] &&
  396.          strncmp (hostname_list[i], text, len) == 0) i++;
  397.       end = i;
  398.  
  399.       result = (char **)xmalloc ((1 + (end - begin)) * sizeof (char *));
  400.       for (i = 0; i + begin < end; i++)
  401.         result[i] = hostname_list[begin + i];
  402.       result[i] = (char *)NULL;
  403.       return (result);
  404.     }
  405.  
  406.       last_search = i;
  407.  
  408.       if (r < 0)
  409.     begin = i;
  410.       else
  411.     end = i;
  412.     }
  413.   return ((char **)NULL);
  414. }
  415.  
  416. /* This is a K*rn shell style insert-last-arg function.  The
  417.    difference is that Bash puts stuff into the history file before
  418.    expansion and file name generation, so we deal with exactly what the
  419.    user typed.  Those wanting the other behavior, at least for the last
  420.    arg, can use `$_'.  This also `knows' about how rl_yank_nth_arg treats
  421.    `$'. */
  422. static void
  423. insert_last_arg (count, c)
  424.      int count, c;
  425. {
  426.   extern int rl_explicit_arg;
  427.  
  428.   if (rl_explicit_arg)
  429.     rl_yank_nth_arg (count, c);
  430.   else
  431.     rl_yank_nth_arg ('$', c);
  432. }
  433.  
  434. /* The equivalent of the K*rn shell C-o operate-and-get-next-history-line
  435.    editing command. */
  436. static int saved_history_line_to_use = 0;
  437.  
  438. static void
  439. set_saved_history ()
  440. {
  441.   HIST_ENTRY *h;
  442.  
  443.   if (saved_history_line_to_use)
  444.     {
  445.       if (history_set_pos (saved_history_line_to_use))
  446.     {
  447.       h = current_history ();
  448.       if (h)
  449.         {
  450.           rl_insert_text (h->line);
  451.           /*
  452.            * Get rid of any undo list created by the previous insert,
  453.            * so the line won't totally be erased when the edits are
  454.            * undone (they will be normally, because this is a history
  455.            * line -- cf. readline.c: line 380 or so).
  456.            */
  457.           if (rl_undo_list)
  458.         {
  459.           free_undo_list ();
  460.           rl_undo_list = (UNDO_LIST *)NULL;
  461.         }
  462.         }
  463.     }
  464.     }
  465.   saved_history_line_to_use = 0;
  466.   rl_startup_hook = (Function *)NULL;
  467. }  
  468.  
  469. static void
  470. operate_and_get_next (count, c)
  471.      int count, c;
  472. {
  473.   int where;
  474.   extern int history_stifled, history_length, max_input_history;
  475.  
  476.   /* Accept the current line. */
  477.   rl_newline ();    
  478.  
  479.   /* Find the current line, and find the next line to use. */
  480.   where = where_history ();
  481.  
  482.   if (history_stifled && (history_length >= max_input_history))
  483.     saved_history_line_to_use = where;
  484.   else
  485.     saved_history_line_to_use = where + 1;
  486.  
  487.   rl_startup_hook = (Function *)set_saved_history;
  488. }
  489.  
  490. /* **************************************************************** */
  491. /*                                    */
  492. /*            How To Do Shell Completion            */
  493. /*                                    */
  494. /* **************************************************************** */
  495.  
  496. /* Do some completion on TEXT.  The indices of TEXT in RL_LINE_BUFFER are
  497.    at START and END.  Return an array of matches, or NULL if none. */
  498. static char **
  499. attempt_shell_completion (text, start, end)
  500.      char *text;
  501.      int start, end;
  502. {
  503.   int in_command_position = 0;
  504.   char **matches = (char **)NULL;
  505.   char *command_separator_chars = ";|&{(";
  506.  
  507.   /* Determine if this could be a command word. */
  508.   if (start == 0)
  509.     in_command_position++;
  510.   else
  511.     {
  512.       register int ti = start - 1;
  513.  
  514.       while (whitespace (rl_line_buffer[ti]) && ti > -1)
  515.     ti--;
  516.  
  517.       if (ti < 0)
  518.     in_command_position++;
  519.       else
  520.     {
  521.       if (member (rl_line_buffer[ti], command_separator_chars) ||
  522.           member (*text, command_separator_chars))
  523.         in_command_position++;
  524.     }
  525.     }
  526.  
  527.   /* Variable name? */
  528.   if (*text == '$')
  529.     {
  530.       matches = completion_matches (text, variable_completion_function);
  531.     }
  532.  
  533.   /* If the word starts in `~', and there is no slash in the word, then
  534.      try completing this word as a username. */
  535.   if (!matches && *text == '~' && !index (text, '/'))
  536.     {
  537.       matches = completion_matches (text, username_completion_function);
  538.     }
  539.  
  540.   /* Another one.  Why not?  If the word starts in '@', then look through
  541.      the world of known hostnames for completion first. */
  542.   if (!matches && *text == '@')
  543.     {
  544.       matches = completion_matches (text, hostname_completion_function);
  545.     }
  546.  
  547.   /* And last, (but not least) if this word is in a command position, then
  548.      complete over possible command names, including aliases, functions,
  549.      and command names. */
  550.  
  551.   if (!matches && in_command_position)
  552.     {
  553.       int text_offset = 0;
  554.  
  555.       if (start && member (*text, command_separator_chars))
  556.     text_offset++, start++;
  557.  
  558.       if (*text != '/')
  559.     matches = completion_matches (&text[text_offset],
  560.                       command_word_completion_function);
  561.     }
  562.   return (matches);
  563. }
  564.  
  565. /* This is the function to call when the word to complete is at the start
  566.    of a line.  It grovels $PATH, looking for commands that match.  It also
  567.    scans for aliases, function names, and the shell_builtin table. */
  568. static char *
  569. command_word_completion_function (hint_text, state)
  570.      char *hint_text;
  571.      int state;
  572. {
  573.   static char *hint = (char *)NULL;
  574.   static char *path = (char *)NULL;
  575.   static char *val = (char *)NULL;
  576.   static char *filename_hint = (char *)NULL;
  577.   char *extract_colon_unit ();
  578.   static int path_index, hint_len, istate;
  579.   static int mapping_over, local_index;
  580.   static SHELL_VAR **varlist = (SHELL_VAR **)NULL;
  581.   extern SHELL_VAR **all_visible_functions ();
  582.  
  583.   /* We have to map over the possibilities for command words.  If we have
  584.      no state, then make one just for that purpose. */
  585.  
  586.   if (!state)
  587.     {
  588.       if (hint)
  589.     free (hint);
  590.  
  591.       path = get_string_value ("PATH");
  592.       path_index = 0;
  593.  
  594.       hint = savestring (hint_text);
  595.       hint_len = strlen (hint);
  596.  
  597.       mapping_over = 0;
  598.       val = (char *)NULL;
  599.  
  600.       /* Initialize the variables for each type of command word. */
  601.       local_index = 0;
  602.  
  603.       if (varlist)
  604.     free (varlist);
  605.  
  606.       varlist = all_visible_functions ();
  607.     }
  608.  
  609.   /* mapping_over says what we are currently hacking.  Note that every case
  610.      in this list must fall through when there are no more possibilities. */
  611.  
  612.   switch (mapping_over)
  613.     {
  614.     case 0:            /* Aliases come first. */
  615. #if defined (ALIAS)
  616.       while (aliases && aliases[local_index])
  617.     {
  618.       register char *alias;
  619.  
  620.       alias = aliases[local_index++]->name;
  621.  
  622.       if (strncmp (alias, hint, hint_len) == 0)
  623.         return (savestring (alias));
  624.     }
  625. #endif /* ALIAS */
  626.       local_index = 0;
  627.       mapping_over++;
  628.  
  629.     case 1:            /* Then function names. */
  630.       while (varlist && varlist[local_index])
  631.     {
  632.       register char *varname;
  633.  
  634.       varname = varlist[local_index++]->name;
  635.  
  636.       if (strncmp (varname, hint, hint_len) == 0)
  637.         return (savestring (varname));
  638.     }
  639.       local_index = 0;
  640.       mapping_over++;
  641.  
  642.     case 2:            /* Then shell builtins. */
  643.       while (shell_builtins[local_index].function)
  644.     {
  645.       register char *builtin_name;
  646.  
  647.       if (!shell_builtins[local_index].enabled)
  648.         {
  649.           local_index++;
  650.           continue;
  651.         }
  652.  
  653.       builtin_name = shell_builtins[local_index++].name;
  654.  
  655.       if (strncmp (builtin_name, hint, hint_len) == 0)
  656.         return (savestring (builtin_name));
  657.     }
  658.       local_index = 0;
  659.       mapping_over++;
  660.     }
  661.  
  662.   /* Repeatedly call filename_completion_function while we have
  663.      members of PATH left.  Question:  should we stat each file?
  664.      Answer: we call executable_file () on each file. */
  665.  outer:
  666.  
  667.   istate = (val != (char *)NULL);
  668.  
  669.   if (!istate)
  670.     {
  671.       char *current_path;
  672.  
  673.       /* Get the next directory from the path.  If there is none, then we
  674.      are all done. */
  675.       if (!path ||
  676.       !path[path_index] ||
  677.       !(current_path = extract_colon_unit (path, &path_index)))
  678.     return ((char *)NULL);
  679.  
  680.       if (!*current_path)
  681.     {
  682.       free (current_path);
  683.       current_path = savestring (".");
  684.     }
  685.  
  686.       if (filename_hint)
  687.     free (filename_hint);
  688.  
  689.       filename_hint =
  690.     (char *)xmalloc (2 + strlen (current_path)
  691.              + strlen (hint));
  692.       sprintf (filename_hint, "%s/%s", current_path, hint);
  693.  
  694.       free (current_path);
  695.     }
  696.  
  697.  inner:
  698.   val = filename_completion_function (filename_hint, istate);
  699.   istate = 1;
  700.  
  701.   if (!val)
  702.     {
  703.       goto outer;
  704.     }
  705.   else
  706.     {
  707.       char *rindex (), *temp = rindex (val, '/');
  708.       temp++;
  709.       if ((strncmp (hint, temp, hint_len) == 0) && executable_file (val))
  710.     {
  711.       temp = (savestring (temp));
  712.       free (val);
  713.       val = "";    /* So it won't be NULL. */
  714.       return (temp);
  715.     }
  716.       else
  717.     {
  718.       free (val);
  719.       goto inner;
  720.     }
  721.     }
  722. }
  723.  
  724. /* Okay, now we write the entry_function for variable completion. */
  725. static char *
  726. variable_completion_function (text, state)
  727.      int state;
  728.      char *text;
  729. {
  730.   register SHELL_VAR *var = (SHELL_VAR *)NULL;
  731.   static SHELL_VAR **varlist = (SHELL_VAR **)NULL;
  732.   static int varlist_index;
  733.   static char *varname = (char *)NULL;
  734.   static int namelen;
  735.   static int first_char, first_char_loc;
  736.  
  737.   extern SHELL_VAR **all_visible_variables ();
  738.  
  739.   if (!state)
  740.     {
  741.       if (varname)
  742.     free (varname);
  743.  
  744.       first_char_loc = 0;
  745.       first_char = text[0];
  746.  
  747.       if (first_char == '$')
  748.     first_char_loc++;
  749.  
  750.       varname = savestring (&text[first_char_loc]);
  751.  
  752.       namelen = strlen (varname);
  753.       if (varlist)
  754.     free (varlist);
  755.       varlist = all_visible_variables ();
  756.       varlist_index = 0;
  757.     }
  758.  
  759.   while (varlist && varlist[varlist_index])
  760.     {
  761.       var = varlist[varlist_index];
  762.  
  763.       /* Compare.  You can't do better than Zayre.  No text is also
  764.      a match.  */
  765.       if (!*varname || (strncmp (varname, var->name, namelen) == 0))
  766.     break;
  767.       varlist_index++;
  768.     }
  769.  
  770.   if (!varlist || !varlist[varlist_index])
  771.     {
  772.       return ((char *)NULL);
  773.     }
  774.   else
  775.     {
  776.       char *value = (char *)xmalloc (2 + strlen (var->name));
  777.  
  778.       if (first_char_loc)
  779.     *value = first_char;
  780.  
  781.       strcpy (&value[first_char_loc], var->name);
  782.  
  783.       varlist_index++;
  784.       return (value);
  785.     }
  786. }
  787.  
  788. /* How about a completion function for hostnames? */
  789. static char *
  790. hostname_completion_function (text, state)
  791.      int state;
  792.      char *text;
  793. {
  794.   static char **list = (char **)NULL;
  795.   static int list_index = 0;
  796.   static int first_char, first_char_loc;
  797.  
  798.   /* If we don't have any state, make some. */
  799.   if (!state)
  800.     {
  801.       char **hostnames_matching ();
  802.  
  803.       if (list)
  804.     free (list);
  805.  
  806.       list = (char **)NULL;
  807.  
  808.       first_char_loc = 0;
  809.       first_char = *text;
  810.  
  811.       if (first_char == '@')
  812.     first_char_loc++;
  813.  
  814.       list = hostnames_matching (&text[first_char_loc]);
  815.       list_index = 0;
  816.     }
  817.  
  818.   if (list && list[list_index])
  819.     {
  820.       char *t = (char *)xmalloc (2 + strlen (list[list_index]));
  821.  
  822.       *t = first_char;
  823.       strcpy (t + first_char_loc, list[list_index]);
  824.       list_index++;
  825.       return (t);
  826.     }
  827.   else
  828.     return ((char *)NULL);
  829. }
  830.  
  831. /* History and alias expand the line.  But maybe do more?  This
  832.    is a test to see what users like.  Do expand_string on the string. */
  833. static void
  834. shell_expand_line (ignore)
  835.      int ignore;
  836. {
  837.   char *pre_process_line (), *new_line;
  838.  
  839.   new_line = pre_process_line (rl_line_buffer, 0, 0);
  840.  
  841. #if defined (ALIAS)
  842.   if (new_line)
  843.     {
  844.       char *alias_expand (), *alias_line;
  845.  
  846.       alias_line = alias_expand (new_line);
  847.       free (new_line);
  848.       new_line = alias_line;
  849.     }
  850. #endif /* ALIAS */
  851.  
  852.   if (new_line)
  853.     {
  854.       int old_point = rl_point;
  855.       int at_end = rl_point == rl_end;
  856.  
  857.       /* If the line was history and alias expanded, then make that
  858.      be one thing to undo. */
  859.  
  860.       if (strcmp (new_line, rl_line_buffer) != 0)
  861.     {
  862.       rl_point = rl_end;
  863.  
  864.       rl_add_undo (UNDO_BEGIN, 0, 0, 0);
  865.       rl_kill_text (0, rl_point);
  866.       rl_point = rl_end = 0;
  867.       rl_insert_text (new_line);
  868.       rl_add_undo (UNDO_END, 0, 0, 0);
  869.     }
  870.  
  871.       free (new_line);
  872.  
  873.       /* If there is variable expansion to perform, do that as a separate
  874.      operation to be undone. */
  875.       {
  876.     extern char *string_list ();
  877.     extern WORD_LIST *expand_string ();
  878.     WORD_LIST *expanded_string;
  879.     char *string_list ();
  880.  
  881.     expanded_string = expand_string (rl_line_buffer, 0);
  882.     if (!expanded_string)
  883.       new_line = savestring ("");
  884.     else
  885.       {
  886.         new_line = string_list (expanded_string);
  887.         dispose_words (expanded_string);
  888.       }
  889.  
  890.       if (strcmp (new_line, rl_line_buffer) != 0)
  891.     {
  892.       rl_add_undo (UNDO_BEGIN, 0, 0 ,0);
  893.       rl_kill_text (0, rl_end);
  894.       rl_point = rl_end = 0;
  895.       rl_insert_text (new_line);
  896.       rl_add_undo (UNDO_END, 0, 0, 0);
  897.     }
  898.  
  899.       free (new_line);
  900.  
  901.       /* Place rl_point where we think it should go. */
  902.       if (at_end)
  903.     rl_point = rl_end;
  904.       else if (old_point < rl_end)
  905.     {
  906.       rl_point = old_point;
  907.       if (!whitespace (rl_line_buffer[rl_point]))
  908.         rl_forward_word (1);
  909.     }
  910.       }
  911.     }
  912.   else
  913.     {
  914.       /* There was an error in expansion.  Let the preprocessor print
  915.      the error here.  Note that we know that pre_process_line ()
  916.      will return NULL, since it just did. */
  917.       fprintf (rl_outstream, "\n\r");
  918.       pre_process_line (rl_line_buffer, 1, 0);
  919.       rl_forced_update_display ();
  920.     }
  921. }
  922.  
  923. /* Filename completion ignore.  Emulates the "fignore" facility of
  924.    tcsh.  If FIGNORE is set, then don't match files with the
  925.    given suffixes.  If only one of the possabilities has an acceptable
  926.    suffix, delete the others, else just return and let the completer
  927.    signal an error.  It is called by the completer when real
  928.    completions are done on filenames by the completer's internal
  929.    function, not for completion lists (M-?) and not on "other"
  930.    completion types, such as hostnames or commands.
  931.  
  932.    It is passed a NULL-terminated array of (char *)'s that must be
  933.    free()'d if they are deleted.  The first element (names[0]) is the
  934.    least-common-denominator string of the matching patterns (i.e.
  935.    u<TAB> produces names[0] = "und", names[1] = "under.c", names[2] =
  936.    "undun.c", name[3] = NULL).  */
  937.  
  938. struct ign {
  939.   char *val;
  940.   int len;
  941. };
  942.  
  943. static struct ign *ignores;    /* Store the ignore strings here */
  944. static int num_ignores;        /* How many are there? */
  945. static char *last_fignore;    /* Last value of fignore - cached for speed */
  946. extern char *extract_colon_unit (), *get_string_value ();
  947.  
  948. static void
  949. setup_ignore_patterns ()
  950. {
  951.   int numitems, maxitems, ptr;
  952.   char *colon_bit;
  953.   struct ign *p;
  954.   
  955.   char *this_fignore = get_string_value ("FIGNORE");
  956.  
  957.   /* If nothing has changed then just exit now. */
  958.   if (this_fignore &&
  959.       last_fignore && 
  960.       strcmp (this_fignore, last_fignore) == 0 ||
  961.       (!this_fignore && !last_fignore))
  962.     {
  963.       return;
  964.     }
  965.  
  966.   /* Oops.  FIGNORE has changed.  Re-parse it. */
  967.   num_ignores = 0;
  968.  
  969.   if (ignores)
  970.     {
  971.       for (p = ignores; p->val; p++) free(p->val);
  972.       free (ignores);
  973.       ignores = (struct ign*)NULL;
  974.     }
  975.  
  976.   if (last_fignore)
  977.     free (last_fignore);
  978.  
  979.   last_fignore = savestring (this_fignore);
  980.   
  981.   if (!this_fignore)
  982.     return;
  983.  
  984.   numitems = maxitems = ptr = 0;
  985.  
  986.   while (colon_bit = extract_colon_unit (this_fignore, &ptr))
  987.     {
  988.       if (numitems + 1 > maxitems)
  989.     ignores = (struct ign *)
  990.       xrealloc (ignores, (maxitems += 10) * sizeof (struct ign));
  991.  
  992.       ignores[numitems].val = colon_bit;
  993.       ignores[numitems].len = strlen (colon_bit);
  994.       numitems++;
  995.     }
  996.   ignores[numitems].val = NULL;
  997.   num_ignores = numitems;
  998. }
  999.  
  1000. static int
  1001. name_is_acceptable (name)
  1002.      char *name;
  1003. {
  1004.   struct ign *p;
  1005.   int nlen = strlen (name);
  1006.  
  1007.   for (p = ignores; p->val; p++) 
  1008.     {
  1009.       if (nlen > p->len && p->len > 0 && 
  1010.       strcmp (p->val, &name[nlen - p->len]) == 0)
  1011.     return (0);
  1012.     }
  1013.  
  1014.   return (1);
  1015. }
  1016.  
  1017. static void
  1018. filename_completion_ignore (names)
  1019.      char **names;
  1020. {
  1021.   char **p;
  1022.   int idx;
  1023.  
  1024.   setup_ignore_patterns ();
  1025.  
  1026.   if (!num_ignores)
  1027.     return;
  1028.   
  1029.   for (p = names + 1, idx = -1; *p; p++)
  1030.     {
  1031.       if (name_is_acceptable (*p))
  1032.     {
  1033.       if (idx == -1)    /* First match found. */
  1034.         idx = p - names;
  1035.       else
  1036.         return;        /* Too many matches. */
  1037.     }
  1038.     }
  1039.   
  1040.   /* If none are acceptable then let the completer handle it. */
  1041.   if (idx == -1)
  1042.     return;
  1043.  
  1044.   /* Delete all non-matching elements. */
  1045.   free (names[0]); 
  1046.   for (p = names + 1; *p; p++)
  1047.     {
  1048.       if (idx == (p - names))
  1049.     names[0] = *p;
  1050.       else 
  1051.     free (*p);
  1052.  
  1053.       *p = NULL;
  1054.     }
  1055. }
  1056.  
  1057. /* If tilde_expand hasn't been able to expand the text, perhaps it
  1058.    is a special shell expansion.  We handle that here. */
  1059. static char *
  1060. bash_tilde_expand (text)
  1061.      char *text;
  1062. {
  1063.   char *result = (char *)NULL;
  1064.  
  1065.   if (strcmp (text, "-") == 0)
  1066.     result = get_string_value ("OLDPWD");
  1067.   else if (strcmp (text, "+") == 0)
  1068.     result = get_string_value ("PWD");
  1069.  
  1070.   if (result)
  1071.     result = savestring (result);
  1072.  
  1073.   return (result);
  1074. }
  1075.  
  1076. /* Handle symbolic link references while hacking completion. */
  1077. static void
  1078. bash_symbolic_link_hook (dirname)
  1079.      char **dirname;
  1080. {
  1081.   extern int follow_symbolic_links;
  1082.   char *make_absolute (), *temp_dirname;
  1083.  
  1084.   if (follow_symbolic_links && (strcmp (*dirname, ".") != 0))
  1085.     {
  1086.       temp_dirname = make_absolute (*dirname, get_working_directory (""));
  1087.  
  1088.       if (temp_dirname)
  1089.     {
  1090.       free (*dirname);
  1091.       *dirname = temp_dirname;
  1092.     }
  1093.     }
  1094. }
  1095.  
  1096. #if defined (SPECIFIC_COMPLETION_FUNCTIONS)
  1097. static void
  1098. bash_complete_username (ignore, ignore2)
  1099.      int ignore, ignore2;
  1100. {
  1101.   bash_complete_username_internal (TAB);
  1102. }
  1103.  
  1104. static void
  1105. bash_possible_username_completions (ignore, ignore2)
  1106.      int ignore, ignore2;
  1107. {
  1108.   bash_complete_username_internal ('?');
  1109. }
  1110.  
  1111. static void
  1112. bash_complete_username_internal (what_to_do)
  1113.      int what_to_do;
  1114. {
  1115.   bash_specific_completion
  1116.     (what_to_do, (Function *)username_completion_function);
  1117. }
  1118.  
  1119. static void
  1120. bash_complete_filename (ignore, ignore2)
  1121.      int ignore, ignore2;
  1122. {
  1123.   bash_complete_filename_internal (TAB);
  1124. }
  1125.  
  1126. static void
  1127. bash_possible_filename_completions (ignore, ignore2)
  1128.      int ignore, ignore2;
  1129. {
  1130.   bash_complete_filename_internal ('?');
  1131. }
  1132.  
  1133. static void
  1134. bash_complete_filename_internal (what_to_do)
  1135.      int what_to_do;
  1136. {
  1137.   Function  *orig_func;
  1138.   Function *orig_attempt_func;
  1139.   char *orig_rl_completer_word_break_characters;
  1140.   extern char *rl_completer_word_break_characters;
  1141.  
  1142.   orig_func = rl_completion_entry_function;
  1143.   orig_attempt_func = rl_attempted_completion_function;
  1144.   orig_rl_completer_word_break_characters = rl_completer_word_break_characters;
  1145.   rl_completion_entry_function = (Function *)filename_completion_function;
  1146.   rl_attempted_completion_function = (Function *)0x0;
  1147.   rl_completer_word_break_characters = " \t\n\"\'";
  1148.  
  1149.   rl_complete_internal (what_to_do);
  1150.  
  1151.   rl_completion_entry_function = orig_func;
  1152.   rl_attempted_completion_function = orig_attempt_func;
  1153.   rl_completer_word_break_characters = orig_rl_completer_word_break_characters;
  1154. }
  1155.  
  1156. static void
  1157. bash_complete_hostname (ignore, ignore2)
  1158.      int ignore, ignore2;
  1159. {
  1160.   bash_complete_hostname_internal (TAB);
  1161. }
  1162.  
  1163. static void
  1164. bash_possible_hostname_completions (ignore, ignore2)
  1165.      int ignore, ignore2;
  1166. {
  1167.   bash_complete_hostname_internal ('?');
  1168. }
  1169.  
  1170. static void
  1171. bash_complete_variable (ignore, ignore2)
  1172.      int ignore, ignore2;
  1173. {
  1174.   bash_complete_variable_internal (TAB);
  1175. }
  1176.  
  1177. static void
  1178. bash_possible_variable_completions (ignore, ignore2)
  1179.      int ignore, ignore2;
  1180. {
  1181.   bash_complete_variable_internal ('?');
  1182. }
  1183.  
  1184.  
  1185. static void
  1186. bash_complete_command (ignore, ignore2)
  1187.      int ignore, ignore2;
  1188. {
  1189.   bash_complete_command_internal (TAB);
  1190. }
  1191.  
  1192. static void
  1193. bash_possible_command_completions (ignore, ignore2)
  1194.      int ignore, ignore2;
  1195. {
  1196.   bash_complete_command_internal ('?');
  1197. }
  1198.  
  1199. static void
  1200. bash_complete_hostname_internal (what_to_do)
  1201.      int what_to_do;
  1202. {
  1203.   bash_specific_completion
  1204.     (what_to_do, (Function *)hostname_completion_function);
  1205. }
  1206.  
  1207. static void
  1208. bash_complete_variable_internal (what_to_do)
  1209.      int what_to_do;
  1210. {
  1211.   bash_specific_completion
  1212.     (what_to_do, (Function *)variable_completion_function);
  1213. }
  1214.  
  1215. static void
  1216. bash_complete_command_internal (what_to_do)
  1217.      int what_to_do;
  1218. {
  1219.   bash_specific_completion
  1220.     (what_to_do, (Function *)command_word_completion_function);
  1221. }
  1222.  
  1223. static void
  1224. bash_specific_completion (what_to_do, generator)
  1225.      int what_to_do;
  1226.      Function *generator;
  1227. {
  1228.   Function *orig_func;
  1229.   Function *orig_attempt_func;
  1230.  
  1231.   orig_func = rl_completion_entry_function;
  1232.   orig_attempt_func = rl_attempted_completion_function;
  1233.   rl_completion_entry_function = generator;
  1234.   rl_attempted_completion_function = (Function *)0x0;
  1235.  
  1236.   rl_complete_internal (what_to_do);
  1237.  
  1238.   rl_completion_entry_function = orig_func;
  1239.   rl_attempted_completion_function = orig_attempt_func;
  1240. }
  1241.  
  1242. #endif    /* SPECIFIC_COMPLETION_FUNCTIONS */
  1243.  
  1244.